package net.uman.framework.modules.utils;

import java.io.IOException;
import java.util.Properties;

import org.apache.cxf.common.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ApplicationPropertiesHelper {
    private static Logger logger = LoggerFactory.getLogger(ApplicationPropertiesHelper.class);
    private static Properties prop;
    
    static {
        try {
            prop = PropertyUtils.loadProperties("/application.properties");
        } catch (IOException e) {
            logger.error("Loading application.properties file failed...");
            e.printStackTrace();
        }
        
    }
    
    public static String getProperty(String key) {
        return prop.getProperty(key);
    }
    
    public static String getProperty(String key, String defaultValue) {
        return prop.getProperty(key, defaultValue);
    }
    
    public static String getImgFilePath() {
        String imgPath = prop.getProperty("img.file.path");
        if (StringUtils.isEmpty(imgPath)) {
            logger.error("application.properties file doesn't define the key of 'img.file.path' or the value is empty.");
            return null;
        }
        return imgPath;
    }
    
    public static String getAttDocPath() {
        String attPath = prop.getProperty("attachment.file.path");
        if (StringUtils.isEmpty(attPath)) {
            logger.error("application.properties file doesn't define the key of 'attachment.file.path' or the value is empty.");
            return null;
        }
        return attPath;
    }
    
    public static String getContractFilePath() {
        String conPath = prop.getProperty("contract.doc.path");
        if (StringUtils.isEmpty(conPath)) {
            logger.error("application.properties file doesn't define the key of 'contract.doc.path' or the value is empty.");
            return null;
        }
        return conPath;
    }
    
    public static String getTempFilePath() {
        String tempPath = prop.getProperty("temp.file.path");
        if (StringUtils.isEmpty(tempPath)) {
            logger.error("application.properties file doesn't define the key of 'temp.file.path' or the value is empty.");
            return null;
        }
        return tempPath;
    }
    
    /**
     * URL format: [img.server.url]/ (e.g. http://domain_name:port/context/)
     * 
     * @return image server URL
     */
    public static String getImgServerUrl() {
        String url = prop.getProperty("img.server.url");
        if (StringUtils.isEmpty(url)) {
            logger.error("application.properties file doesn't define the key of 'img.server.url' or the value is empty.");
            return null;
        }
        return url.endsWith("/") ? url : url + "/";
    }
    
}
